<?php
App::import('vendor','paginator');
class AjaxController extends AppController
{
	var $name = "Ajax";
	var $uses = array('Ajax','Page', 'Cron');
	var $components = array('Session');
	var $metaDescription;
	var $metaKeywords;
	var $layout = "default";
	var $currency = "&euro;";
		
	function setPriceForBuilding($bcond, $region_worth, $btype){
		$bprice = $this->Cron->getValue4Building($bcond, $region_worth, $btype);
		echo "<input type='hidden' id='bprice' name='bprice' value='{$bprice}'/>
			<div class='label_lf'>Building Price: </div>
			<div class='field_lf'><p>{$bprice}{$this->currency}</p></div>
			<div class='clear'>&nbsp;</div>";
		exit();	
	}
	
	function setTheGame($game_id) {
		$this->Session->write('pre_group_id', $game_id);
		exit();
	}
	
	function getGameFriends($game_id) {
		$agame = $this->Ajax->getGameGroup($game_id);
		$str = "<div class='one_game'>";
		if(count($agame)%2==0){
			$col_size =	(int)(count($agame)/2)-1;
		}else {
			$col_size =	(int)(count($agame)/2);
		} 
		for($i=0;$i<count($agame);$i++){
			$str.="<span class='strong'>&raquo;</span> ".$agame[$i]['first_name']." ".
				$agame[$i]['last_name']."<br/>";
			if($i == $col_size) {	
				$str .= "</div><div class='one_game'>";
			}		
		}
		$str .= "</div><div class='clear'>&nbsp;</div>";
		echo $str;
		exit();
	}
	
	function displayProperty($lat,$long){
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
		$marker = "";
		//if in DB, select info
		$game_id     = $this->Session->read('game_id');
		$is_property = $this->Ajax->searchProperty($lat,$long,$game_id);		
		if($is_property == 0){
			$region = $this->Ajax->getRegion($lat,$long,$game_id);			
			$building_condition = $this->Ajax->getDataTypes("building_condition");
			$bcond = $building_condition[rand(0, count($building_condition)-1)];		
			while($bcond<10){
				$bcond = $building_condition[rand(0, count($building_condition)-1)];		
			}
			$prop_value = $this->Cron->getValue4Building((int)$bcond,(int)$region["region_worth"],'house');
			
			$rev_val = $this->Cron->getValue4Revenue($bcond,$region["region_worth"],1,$region["region_id"]);
			$region_id = $region["region_id"] % 100;
			if(!empty($region)){
				$marker.="<table>\n      
					<tr><td>\n        
						<div style='width:180px; font-family: Arial; font-size:8px;'>\n
					         <p><span class='strong'>Region:</span>".$region_id."</p>\n         
							<p><span class='strong'>Owner:</span> FOR SALE!</p>\n         
					         <p><span class='strong'>Property:</span> empty</p>\n         
					         <p><span class='strong'>Revenue:</span> {$this->currency} 
									{$rev_val}/day</p>\n          
					         <p><span class='strong'>Buy Price:</span> approx {$prop_value} {$this->currency}</p>\n          
					         <p><span class='strong'>Condition:</span> {$bcond}%</p>\n        
						</div>\n       
					</td><td>\n        
						 <div style='text-align: center;'>\n        
					 		 <img src='img/property/forsale.jpg' style='border:2px solid #cecece;'>\n        
					 	 	<form action='".U_HOME."Pages/buyCreateProperty/{$lat}/{$long}/{$bcond}/' method='post'>\n          
					 	 		<input type='submit' value='\$Buy\$'>\n         
					 	 	</form>\n        
					 </div>\n       
					 </td></tr>\n
					 </table>^0^house";
			}	
		} 
		echo $marker;
		exit();
	}
	
	function buyProperty($marker_id, $prop_value){
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
	
		$user_id = $this->Session->read('logged');
		$message = $this->Page->affordable($prop_value,$user_id);
		if (empty($message)) {			
			$region = $this->Ajax->buyProperty($marker_id,$user_id,$prop_value);
			echo 'Property succesfully bought!^0';
			$this->requestAction('/Cron/createMarkers/');
		} else {
			echo $message."^1";	
		}
		exit();
	}
	
	function setForsale($property_id, $value){
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
	
		$user_id = $this->Session->read('logged');
		$this->Ajax->query("UPDATE `{$this->Ajax->userProp}` 
				SET `forsale`={$value} 
				WHERE `property_id` = {$property_id} 
					AND `user_id`= {$user_id}");
		echo "The information was successfully changed";	
		exit();
	}	
	
	function getDestructionInfo($destruction_name){
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
	
		$destruction = $this->Ajax->getDestruction($destruction_name);
		echo "var destructions = new Array();\n".
			"destructions[0] = '{$destruction['buy_description']}';\n".
			"destructions[1] = '{$destruction['destruction_img']}';\n".
			"destructions[2] = '{$destruction['destruction_impact']}';\n".
			"destructions[3] = '{$destruction['destruction_price']}';\n".
			"destructions[4] = '{$destruction['destruction_id']}';\n".
			"destructions[5] = '{$destruction['destruction_duration']}';\n";
		exit();
	}
	

	function doTheChatWrite($nickname){
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
		$msg = trim(strip_tags($this->params['form']['message']));
    	$id  = $this->Session->read('logged');	
    	$chat_id = $this->Ajax->insertChatMessage($msg,$id,$nickname);
    	$this->Session->write('last_chat_id', $chat_id);
    	$data = array('message' => $msg, 'nickname' => $nickname);
    	if(!empty($msg)) {
        	echo json_encode($data);
    	} else {
        	echo json_encode('');
    	}
    	exit();
	}

	function doTheChatRead($nickname) {
		if(! $this->Session->check('logged')){
			$this->redirect(U_HOME);
		}
		if(!$this->Session->read('last_chat_id')){
			$this->Session->write('last_chat_id',0);
		}
		$chat_id = $this->Session->read('last_chat_id');
		$game_id = $this->Session->read('game_id');		
		$get_the_last = $this->Ajax->readLast50Chats((int)$chat_id,$nickname,$game_id);
			
		if(count($get_the_last)>0){
    		$this->Session->write('last_chat_id', $get_the_last[count($get_the_last)-1]['chat_id']);		
    		echo json_encode($get_the_last);
    	} else {
    	 	echo json_encode('');
    	}	
    	exit();
	}
}



